package com.lhf.userservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lhf.common.util.Result;
import com.lhf.userservice.entity.FvsSysRole;
import com.lhf.userservice.service.FvsSysRoleService;
import com.lhf.userservice.vo.FvsSysAddPerForRoleVo;
import com.lhf.userservice.vo.FvsSysRoleVO;
import com.lhf.userserviceapi.dto.FvsSysRoleDTO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * (FvsSysRole)表控制层1
 *
 * @author liuhf
 * @since 2021-03-30 08:44:55
 */
@RestController
@RequestMapping("fvsSysRole")
public class FvsSysRoleController {
    /**
     * 服务对象
     */
    @Resource
    private FvsSysRoleService fvsSysRoleService;

    /**
     * 分页查询所有数据
     *
     * @param page       分页对象
     * @param fvsSysRole 查询实体
     * @return 所有数据
     */
    @GetMapping("selectByLimit")
    public Result<IPage<FvsSysRole>> selectAll(Page<FvsSysRole> page, FvsSysRole fvsSysRole) {
        return Result.success(this.fvsSysRoleService.page(page, new QueryWrapper<>(fvsSysRole)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne/{id}")
    public Result<FvsSysRole> selectOne(@PathVariable Serializable id) {
        return Result.success(this.fvsSysRoleService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param fvsSysRole 实体对象
     * @return 新增结果
     */
    @PostMapping("save")
    public Result<Boolean> insert(@RequestBody FvsSysRole fvsSysRole) {
        System.out.println(fvsSysRole);
        return Result.success(this.fvsSysRoleService.save(fvsSysRole));
    }

    /**
     * 修改数据
     *
     * @param fvsSysRole 实体对象
     * @return 修改结果
     */
    @PutMapping("modify")
    public Result<Boolean> update(@RequestBody FvsSysRole fvsSysRole) {
        System.out.println(fvsSysRole);
        return Result.success(this.fvsSysRoleService.updateById(fvsSysRole));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @DeleteMapping("del/{id}")
    public Result<Boolean> delete(@PathVariable("id") Long id) {
        return Result.success(this.fvsSysRoleService.removeById(id));
    }

    /**
     * 查询用户所有角色
     *
     * @param uid
     * @return
     */
    @GetMapping("findAllByUid/{uid}")
    public Result<List<FvsSysRoleVO>> findAllByUid(@PathVariable Long uid) {
        return Result.success(this.fvsSysRoleService.findAllByUid(uid));
    }

    /**
     * 为角色添加权限
     *
     * @param vo
     * @return
     */
    @PostMapping("addPerForRole")
    public Result<Boolean> addPerForRole(@RequestBody FvsSysAddPerForRoleVo vo) {
        return Result.success(this.fvsSysRoleService.addPerForRole(vo));
    }

    /**
     * 删除角色权限
     *
     * @param vo
     * @return
     */
    @DeleteMapping("delPerForRole")
    public Result<Boolean> delPerForRole(@RequestBody FvsSysAddPerForRoleVo vo) {
        return Result.success(this.fvsSysRoleService.delPerForRole(vo));
    }

    /**
     * 根据路径查询请求该路径所需要的角色
     *
     * @param path
     * @return
     */
    @GetMapping("findRoleByPath")
    public Result<List<FvsSysRoleDTO>> findRoleByPath(@RequestParam String path) {
        return Result.success(this.fvsSysRoleService.findRoleByPath(path));
    }

}
